package com.example.haojiutao.controller.user;

import com.example.haojiutao.common.Result;
import com.example.haojiutao.config.AliReturnPay;
import com.example.haojiutao.config.AlipayConfig;
import com.example.haojiutao.model.domain.Order;
import com.example.haojiutao.model.vo.response.OrderGoodsDetailVO;
import com.example.haojiutao.model.vo.response.ReOrderGoodsDetailVO;
import com.example.haojiutao.service.GoodsService;
import com.example.haojiutao.service.OrderService;
import com.example.haojiutao.service.ReturnOrderService;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 退货类控制层
 * @Author: zhou_yuan
 * @CreateTime： 2024/2/5 13:43
 */
@Slf4j
@RestController
public class ReturnOrderController {
    @Autowired
    private ReturnOrderService reOrderService;
    @Autowired
    private OrderService orderService;


    /*
     * @Description: 用户下单，通过在商品详情页下单，买卖填写地址、电话等信息，提交订单
     * @Author: zhou_yuan
     * @CreateTime： 2024/2/5 13:45
     * @param: order
     * @return: com.example.haojiutao.common.Result
     */
    @PostMapping("/user/returnorder/{orderNumber}")
    public Result insertOrder(@RequestBody @PathVariable Integer orderNumber) {
        ReOrderGoodsDetailVO reorder = new ReOrderGoodsDetailVO();
        OrderGoodsDetailVO order = orderService.selectByOrderNumber(orderNumber);
        reorder.setOrderNumber(order.getOrderNumber());
        reorder.setGoodsName(order.getGoodsName());
        reorder.setSellerName(order.getSellerName());
        reorder.setOrderPrice(order.getOrderPrice());
        reorder.setUserName(order.getUserName());
        reorder.setGoodsId(order.getGoodsId());
        reorder.setSellerName(order.getGoodsName());
        reorder.setAmount(order.getAmount());
        log.info("新增订单；{}", order);
        System.out.println("___________________" + order.getOrderNumber());
        return reOrderService.insertRetrunOrder(reorder);
    }

    /*
     * @Description: 确认收货接口,卖家收货后点击签收按钮，后台修改订单状态为“已签收”
     * @Author: zhou_yuan
     * @CreateTime： 2024/2/5 13:50
     * @param: order
     * @return: com.example.haojiutao.common.Result
     */
    @PutMapping("/user/returnorder/orderstatus/{orderNumber}")
    public Result updateOrder(@PathVariable Integer reorderId) {
        ReOrderGoodsDetailVO reorder = new ReOrderGoodsDetailVO();
        log.info("更改收货状态");
        reorder.setReorderId(reorderId);
        return reOrderService.updateReturnOrder(reorder);
    }
    /*
     * @Description:买家添加物流单号，更改订单状态为待收货
     * @Author: zhou_yuan
     * @CreateTime： 2024/2/5 13:51
     * @param: order
     * @return: com.example.haojiutao.common.Result
     */
    @PutMapping("/user/returnorder/Tracking/{reorderId}")
    public Result updateOrderLogistics(@RequestBody @PathVariable Integer reorderId, ReOrderGoodsDetailVO reorder1) {
        ReOrderGoodsDetailVO reorder = new ReOrderGoodsDetailVO();
        log.info("买家添加物流信息");
        reorder.setReorderId(reorderId);
        reorder.setRetrackingNumber(reorder1.getRetrackingNumber());
        System.out.println("==============" + reorder.getRetrackingNumber());
        return reOrderService.updateOrderLogistics(reorder);
    }
    /*
     * @Description:卖家是否同意
     * @Author: zhou_yuan
     * @CreateTime： 2024/2/5 14:00
     * @param: order
     * @return: com.example.haojiutao.common.Result
     */
    @PutMapping("/user/returnorder/agree/{reorderId}")
    public Result updateOrderOpinion(@RequestBody @PathVariable Integer reorderId) {
        ReOrderGoodsDetailVO reorder = new ReOrderGoodsDetailVO();
        log.info("卖家同意退货");
        reorder.setReorderId(reorderId);
        return reOrderService.updateOrderOpinion(reorder);
    }

    /*
     * @Description:查看订单
     * @Author: zhou_yuan
     * @CreateTime： 2024/2/5 14:06
     * @param: orderNumber
     * @return: com.example.haojiutao.common.Result
     */
    @GetMapping("/user/returnorder/reorderId/{reorderId}")
    public Result selectByReOrderNumber(@PathVariable Integer reorderId) {
        log.info("查看订单");
        ReOrderGoodsDetailVO order = new ReOrderGoodsDetailVO();
        order= reOrderService.selectByReOrderNumber(reorderId);
        return Result.success("查询成功", order);
    }

    /*
     * @Description: 显示个人退款订单列表
     * @Author: zhou_yuan
     * @CreateTime： 2024/2/5 14:10
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */

    @GetMapping("/user/returnorder/{userName}")
    public Result selectList(@PathVariable String userName) {
        ReOrderGoodsDetailVO reorder = new ReOrderGoodsDetailVO();
        reorder.setUserName(userName);
        List<OrderGoodsDetailVO> order1 = reOrderService.selectListByUser(reorder);
        PageInfo<OrderGoodsDetailVO> pageInfo = new PageInfo<>(order1);
        return Result.success("查询成功", pageInfo);
    }

    /*
     * @Description: 取消订单，当买家未付款或未成功付款时，实现：使用时间戳，当时间流逝完时执行取消订单
     *
     * @Author: zhou_yuan
     * @CreateTime： 2024/2/5 14:26
     * @param: orderNumber
     * @return: com.example.haojiutao.common.Result
     **/
    @DeleteMapping("/user/returnorder/{reorderId}")
    public Result delete1(@PathVariable("reorderId") Integer reorderId) {
        return reOrderService.deleteById(reorderId);
    }


}
